<template>
    <div class="app-container">
      <el-form ref="authForm" :model="form" v-if="open" :rules="rules" label-width="100px">
          <!-- 认证方式选择 -->
          <el-form-item label="认证方式" prop="authType">
            <el-radio-group v-model="form.authType" @change="handleAuthTypeChange">
              <el-radio label="invite">邀请码认证</el-radio>
              <el-radio label="normal">普通认证</el-radio>
            </el-radio-group>
          </el-form-item>

          <!-- 邀请码认证表单项 -->
          <div v-if="form.authType === 'invite'">
            <el-form-item label="邀请码" prop="inviteCode">
              <el-input
                v-model="form.inviteCode"
                placeholder="请输入邀请码"
                clearable
                style="width: 300px"
              />
            </el-form-item>
          </div>

          <!-- 普通认证表单项 -->
          <div v-if="form.authType === 'normal'">
            <el-form-item label="公司名称" prop="companyName">
              <el-input
                v-model="form.companyName"
                placeholder="请输入公司全称"
                clearable
                style="width: 300px"
              />
            </el-form-item>

            <el-form-item label="营业执照" prop="businessLicense">
              <file-upload
                v-model="form.businessLicense"
                :limit="1"
                accept=".jpg,.png,.pdf"
                tip="请上传营业执照扫描件"
              />
            </el-form-item>

            <el-form-item label="注册信息" prop="registration">
              <file-upload
                v-model="form.registration"
                :limit="1"
                accept=".jpg,.png,.pdf"
                tip="请上传注册信息文件"
              />
            </el-form-item>

            <el-form-item label="电子合同" prop="contract">
              <file-upload
                v-model="form.contract"
                :limit="1"
                accept=".pdf,.doc,.docx"
                tip="请上传电子合同文件"
              />
            </el-form-item>
          </div>

          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">提交认证</el-button>
            <el-button @click="cancel">取消</el-button>
          </div>
        </el-form>
        <!-- 信息提交后状态 -->
        <el-form ref="authForm" :model="formData" v-else  label-width="100px">

          <!-- 普通认证表单项 -->
          <div>
            <el-form-item label="公司名称" prop="companyName">
              <el-input
                v-model="formData.companyName"
                :readonly="true"
                style="width: 300px"
              />
            </el-form-item>

            <el-form-item label="营业执照" prop="businessLicense">
              <el-button @click="downloadFile(formData.businessLicense)">文件下载</el-button>
            </el-form-item>

            <el-form-item label="注册信息" prop="registration">
              <el-button @click="downloadFile(formData.registration)">文件下载</el-button>
            </el-form-item>

            <el-form-item label="电子合同" prop="contract">
              <el-button @click="downloadFile(formData.contract)">文件下载</el-button>
            </el-form-item>
    <el-form-item label="审核状态" prop="status">
    <el-tag :type="getStatusType(formData.status)" disable-transitions>{{ getStatusText(formData.status) }}</el-tag>
  </el-form-item>

  <div class="dialog-footer" v-if="formData.status==='2'">
            <el-button type="primary" @click="update">修改</el-button>
          </div>
</div>
</el-form>

    </div>
  </template>

  <script>
  import { listPurch_info,  updatePurch_info,authPurch_info } from "@/api/system/purch_info";
  import { getUserProfile } from "@/api/system/user";



  export default {
    name: "PurchaserAuth",
    inject: ['globalData'],
    data() {
      return {
        title: "采购商认证",
        open: true,
        isUpdate: false,
        form: {
          authType: "invite", // 默认认证方式
          inviteCode: "",
          companyName: null,
          businessLicense: null,
          registration: null,
          contract: null
        },
        formData:{},
        // 查询参数
        queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaserId: null,
        companyName: null,
        businessLicense: null,
        registration: null,
        contract: null,
        createdAt: null,
        updatedAt: null,
        status: null,
        auditorId: null
      },

        rules: {
          authType: [
            { required: true, message: "请选择认证方式", trigger: "change" }
          ],
          inviteCode: [
            {
              required: true,
              message: "请输入邀请码",
              trigger: "blur",
              validator: (rule, value, callback) => {
                if (this.form.authType === 'invite' && !value) {
                  callback(new Error(rule.message))
                } else {
                  callback()
                }
              }
            }
          ],
          companyName: [
            {
              required: true,
              message: "请输入公司名称",
              trigger: "blur",
              validator: (rule, value, callback) => {
                if (this.form.authType === 'normal' && !value) {
                  callback(new Error(rule.message))
                } else {
                  callback()
                }
              }
            }
          ],
          businessLicense: [
            {
              required: true,
              message: "请上传营业执照",
              trigger: "change",
              validator: (rule, value, callback) => {
                if (this.form.authType === 'normal' && !value) {
                  callback(new Error(rule.message))
                } else {
                  callback()
                }
              }
            }
          ],
          registration: [
            {
              required: true,
              message: "请上传注册信息",
              trigger: "change",
              validator: (rule, value, callback) => {
                if (this.form.authType === 'normal' && !value) {
                  callback(new Error(rule.message))
                } else {
                  callback()
                }
              }
            }
          ],
          contract: [
            {
              required: true,
              message: "请上传电子合同",
              trigger: "change",
              validator: (rule, value, callback) => {
                if (this.form.authType === 'normal' && !value) {
                  callback(new Error(rule.message))
                } else {
                  callback()
                }
              }
            }
          ]
        }
      };
    },
    created() {
      this.checkAuth();
    },
    methods: {
      checkAuth() {
        getUserProfile().then(response => {
          this.queryParams.purchaserId=response.data.userId;
          listPurch_info(this.queryParams).then(response => {
          if(response.rows.length!=0){
            this.open=false;
            this.formData=response.rows[0];
            // this.formData.businessLicense='https://view.officeapps.live.com/op/view.aspx?src='+this.formData.businessLicense;
            // this.formData.registration='https://view.officeapps.live.com/op/view.aspx?src='+this.formData.registration;
            // this.formData.contract='https://view.officeapps.live.com/op/view.aspx?src='+this.formData.contract;
          }
        });
        });
      },
      // 切换认证方式时重置表单
      handleAuthTypeChange() {
        this.$refs.authForm.clearValidate()
        this.form.inviteCode = ""
        this.form.companyName = null
        this.form.businessLicense = null
        this.form.registration = null
        this.form.contract = null
      },

      // 提交表单
      submitForm() {
        this.$refs.authForm.validate(valid => {
          if (valid) {
            if(this.isUpdate&&this.form.authType==='normal'){
              this.formData.status="0";
              this.formData.companyName=this.form.companyName;
              this.formData.businessLicense=this.form.businessLicense;
              this.formData.registration=this.form.registration;
              this.formData.contract=this.form.contract;
              updatePurch_info(this.formData).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("修改成功")
                location.reload();
              } else {
                this.$modal.msgError(res.msg)
              }
            });
            }
            else{
              authPurch_info(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("提交成功")
                location.reload();
              } else {
                this.$modal.msgError(res.msg)
              }
            });
            }

          }
        })
      },
      //修改表单
      update(){
        this.open=true;
        this.isUpdate=true;
      },
      cancel() {
        this.resetForm()
      },

      resetForm() {
        this.form = {
          authType: "invite",
          inviteCode: "",
          companyName: "",
          businessLicense: null,
          registration: null,
          contract: null
        }
        this.$refs.authForm.resetFields()
      },
      getStatusText(status) {
       switch (status) {
      case '0':
        return '未审核';
      case '1':
        return '审核通过';
      case '2':
        return '审核未通过';
      default:
        return '未知状态';
    }
  },
  getStatusType(status) {
    switch (status) {
      case '0':
        return 'info'; // 蓝色
      case '1':
        return 'success'; // 绿色
      case '2':
        return 'danger'; // 红色
      default:
        return '';
    }
    },
    downloadFile(src) {
      var elemIF = document.createElement('iframe');
      elemIF.src = src;
      elemIF.style.display = 'none';
      document.body.appendChild(elemIF);
    }
  }
  };
  </script>
